﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public static class NoiNhanDAO
    {
        public static List<NoiNhanDTO> LoadAll(int maHeThong, bool order)
        {
            List<NoiNhanDTO> result = new List<NoiNhanDTO>();
            SqlConnection connection = new SqlConnection(DataProvider.ConnectionSting);
            try
            {
                SqlCommand command;
                connection.Open();
                if (order)
                {
                    command = new SqlCommand("sp_SelectNoiNhanByHeThongOrder", connection);
                }
                else
                {
                    command = new SqlCommand("sp_SelectNoiNhanByHeThong", connection);
                }
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@maHeThong", SqlDbType.Int).Value = maHeThong;

                SqlDataReader reader;
                reader = command.ExecuteReader();
                Dictionary<int, HeThongDTO> dictHeThong = new Dictionary<int, HeThongDTO>();

                while (reader.Read())
                {
                    NoiNhanDTO tmpNoiNhan = new NoiNhanDTO();
                    HeThongDTO tmpHeThong = new HeThongDTO();
                    tmpNoiNhan.ID = reader.GetInt32(0);
                    tmpNoiNhan.Ten = reader.GetString(1);
                    tmpHeThong.ID = reader.GetInt32(2);
                    tmpHeThong.Ten = reader.GetString(3);
                    tmpNoiNhan.Dem = reader.GetInt32(4);

                    if (!dictHeThong.ContainsKey(tmpHeThong.ID))
                    {
                        dictHeThong.Add(tmpHeThong.ID, tmpHeThong);
                        tmpNoiNhan.HeThong = tmpHeThong;
                    }
                    else
                    {
                        tmpNoiNhan.HeThong = dictHeThong[tmpHeThong.ID];
                    }

                    result.Add(tmpNoiNhan);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
            return result;

        }
        public static List<NoiNhanDTO> Load(int maCongVan)
        {
            List<NoiNhanDTO> result = new List<NoiNhanDTO>();
            SqlConnection connection = new SqlConnection(DataProvider.ConnectionSting);
            try
            {
                SqlCommand command;
                connection.Open();
                command = new SqlCommand("sp_SelectNoiNhanByCongVanDi", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@id", SqlDbType.Int).Value = maCongVan;

                SqlDataReader reader;
                reader = command.ExecuteReader();
                Dictionary<int, HeThongDTO> dictHeThong = new Dictionary<int, HeThongDTO>();

                while (reader.Read())
                {
                    NoiNhanDTO tmpNoiNhan = new NoiNhanDTO();
                    HeThongDTO tmpHeThong = new HeThongDTO();
                    tmpNoiNhan.ID = reader.GetInt32(0);
                    tmpNoiNhan.Ten = reader.GetString(1);
                    tmpHeThong.ID = reader.GetInt32(2);
                    tmpHeThong.Ten = reader.GetString(3);
                    tmpNoiNhan.Dem = reader.GetInt32(4);

                    if (!dictHeThong.ContainsKey(tmpHeThong.ID))
                    {
                        dictHeThong.Add(tmpHeThong.ID, tmpHeThong);
                        tmpNoiNhan.HeThong = tmpHeThong;
                    }
                    else
                    {
                        tmpNoiNhan.HeThong = dictHeThong[tmpHeThong.ID];
                    }

                    result.Add(tmpNoiNhan);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
            return result;

        }
        public static bool Add(NoiNhanDTO tmp)
        {
            bool result = false;
            SqlConnection connection = new SqlConnection(DataProvider.ConnectionSting);

            try
            {
                SqlCommand command = new SqlCommand("sp_InsertNoiNhan", connection);
                command.Parameters.Add("@ten", SqlDbType.NVarChar).Value = tmp.Ten;
                command.Parameters.Add("@mahethong", SqlDbType.Int).Value = tmp.HeThong.ID;
                command.Parameters.Add("@dem", SqlDbType.Int).Value = tmp.Dem;
                command.CommandType = CommandType.StoredProcedure;

                connection.Open();

                if (command.ExecuteNonQuery() != 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
            return result;
        }

        public static bool Edit(NoiNhanDTO tmp)
        {
            bool result = false;
            SqlConnection connection = new SqlConnection(DataProvider.ConnectionSting);

            try
            {
                SqlCommand command = new SqlCommand("sp_UpdateNoiNhan", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ID", SqlDbType.Int).Value = tmp.ID;
                command.Parameters.Add("@ten", SqlDbType.NVarChar).Value = tmp.Ten;
                command.Parameters.Add("@maHeThong", SqlDbType.NVarChar).Value = tmp.HeThong.ID;

                connection.Open();
                if (command.ExecuteNonQuery() != 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
            return result;
        }
    }
}
